﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Subsonic.Data;
using Subsonic.ViewModels;
using MichMan.Utilities;
using System.Collections;

namespace Subsonic.Controls
{
    public class MediaApplicationBar
    {
        public MediaApplicationBar(PhoneApplicationPage parent)
        {
            Page = parent;
            parent.ApplicationBar = (IApplicationBar)Application.Current.Resources["globalApplicationBar"];

            // Hook up events.
            Page.LayoutUpdated += new EventHandler(parent_LayoutUpdated);
            Page.Loaded += new RoutedEventHandler(Page_Loaded);
            Page.Unloaded += (s, e) =>
            {
                App.MediaElement.CurrentStateChanged -= new RoutedEventHandler(MediaElement_CurrentStateChanged);
            };
        }

        Once mediaElementAvailable = new Once(() => App.MediaElement != null);
        void parent_LayoutUpdated(object sender, EventArgs e)
        {
            if (mediaElementAvailable.Value)
            {
                Page.LayoutUpdated -= new EventHandler(parent_LayoutUpdated);
                App.MediaElement.CurrentStateChanged += new RoutedEventHandler(MediaElement_CurrentStateChanged);
                MediaElement_CurrentStateChanged(App.MediaElement, null);
            }
        }

        void MediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            if (appbar_button_playpause == null)
            {
                return;
            }

            MediaElement me = (MediaElement)sender;
            IList buttons = Page.ApplicationBar.Buttons;
            if (me != null && me.CurrentState == System.Windows.Media.MediaElementState.Playing)
            {
                appbar_button_playpause.IconUri = new Uri("/Images/appbar.transport.pause.rest.png", UriKind.Relative);
                appbar_button_playpause.Text = (string)App.Current.Resources["player_pause"];
            }
            else
            {
                appbar_button_playpause.IconUri = new Uri("/Images/appbar.transport.play.rest.png", UriKind.Relative);
                appbar_button_playpause.Text = (string)App.Current.Resources["player_play"];
            }
        }

        internal PhoneApplicationPage Page { get; set; }

        internal ApplicationBarIconButton appbar_button_prev;
        internal ApplicationBarIconButton appbar_button_playpause;
        internal ApplicationBarIconButton appbar_button_next;
        internal ApplicationBarIconButton appbar_button_queue;

        internal ApplicationBarMenuItem appbar_menu_servers;
        internal ApplicationBarMenuItem appbar_menu_settings;
        internal ApplicationBarMenuItem appbar_menu_refresh;
        internal ApplicationBarMenuItem appbar_menu_help;

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            var buttons = Page.ApplicationBar.Buttons.Cast<ApplicationBarIconButton>();

            appbar_button_prev = (ApplicationBarIconButton)Page.ApplicationBar.Buttons[0];
            appbar_button_playpause = (ApplicationBarIconButton)Page.ApplicationBar.Buttons[1];
            appbar_button_next = (ApplicationBarIconButton)Page.ApplicationBar.Buttons[2];
            appbar_button_queue = (ApplicationBarIconButton)Page.ApplicationBar.Buttons[3];

            appbar_menu_servers = ((ApplicationBarMenuItem)(Page.ApplicationBar.MenuItems[0]));
            appbar_menu_settings = ((ApplicationBarMenuItem)(Page.ApplicationBar.MenuItems[1]));
            appbar_menu_refresh = ((ApplicationBarMenuItem)(Page.ApplicationBar.MenuItems[2]));
            appbar_menu_help = ((ApplicationBarMenuItem)(Page.ApplicationBar.MenuItems[3]));

            appbar_button_prev.Click += new EventHandler(appbar_button_prev_Click);
            appbar_button_playpause.Click +=new EventHandler(appbar_button_playpause_Click);
            appbar_button_next.Click +=new EventHandler(appbar_button_next_Click);
            appbar_button_queue.Click +=new EventHandler(appbar_button_queue_Click);

            appbar_menu_servers.Click += new EventHandler(appbar_menu_servers_Click);
            appbar_menu_settings.Click += new EventHandler(appbar_menu_settings_Click);
            appbar_menu_refresh.Click += new EventHandler(appbar_menu_refresh_Click);
            appbar_menu_help.Click += new EventHandler(appbar_menu_help_Click);

            if (Page.NavigationService.CurrentSource.ToString().EndsWith("NowPlayingPivot.xaml"))
            {
                appbar_button_queue.IsEnabled = false;
            }
            else
            {
                appbar_button_queue.IsEnabled = true;
            }
            if (Page.NavigationService.CurrentSource.ToString().EndsWith("ServerSettingsPage.xaml"))
            {
                appbar_menu_servers.IsEnabled = false;
            }
            else
            {
                appbar_menu_servers.IsEnabled = true;
            }
            if (Page.NavigationService.CurrentSource.ToString().EndsWith("SettingsPage.xaml"))
            {
                appbar_menu_settings.IsEnabled = false;
            }
            else
            {
                appbar_menu_settings.IsEnabled = true;
            }
            
            MediaElement_CurrentStateChanged(App.MediaElement, null);
        }

        void  appbar_button_queue_Click(object sender, EventArgs e)
        {
            Page.NavigationService.Navigate(new Uri("/Pages/NowPlayingPivot.xaml", UriKind.Relative));
        }

        void appbar_menu_help_Click(object sender, EventArgs e)
        {
            Page.NavigationService.Navigate(new HelpItem());
        }

        void appbar_menu_refresh_Click(object sender, EventArgs e)
        {
            new ClearCache().DoAction(null);
        }

        private void appbar_button_prev_Click(object sender, EventArgs e)
        {
            MusicPlayer.Instance.Prev();
        }

        Pivot MainPivot
        {
            get
            {
                if (Page != null)
                {
                    return Page.GetFirstDescendant<Pivot>();
                }
                return null;
            }
        }

        private void appbar_button_playpause_Click(object sender, EventArgs e)
        {
            if (this.appbar_button_playpause.IconUri.ToString().EndsWith("play.rest.png"))
            {
                MusicPlayer.Instance.Play();
                // appbar_button_playpause.IconUri = new Uri("/Images/appbar.transport.pause.rest.png", UriKind.Relative);
                // appbar_button_playpause.Text = (string)App.Current.Resources["player_pause"];
            }
            else
            {
                MusicPlayer.Instance.Pause();
                // appbar_button_playpause.IconUri = new Uri("/Images/appbar.transport.play.rest.png", UriKind.Relative);
                // appbar_button_playpause.Text = (string)App.Current.Resources["player_play"];
            }
        }

        private void appbar_button_next_Click(object sender, EventArgs e)
        {
            MusicPlayer.Instance.Skip();
        }

        private void appbar_menu_settings_Click(object sender, EventArgs e)
        {
            Page.NavigationService.Navigate(new SettingsItem());
        }

        private void appbar_menu_servers_Click(object sender, EventArgs e)
        {
            Page.NavigationService.Navigate(new SelectServerItem());
        }
    }
}
